<template>
    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="username"  placeholder="Title" style="width: 200px;" class="filter-item" />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="roleName()">
          查询
        </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
      </div>
      <br/>
      <el-table
        :data="list"
        border
        fit
        highlight-current-row
      >
        <el-table-column  label="ID" >
          <template slot-scope="scope">
            {{ scope.row.rid }}
          </template>
        </el-table-column>
        <el-table-column label="角色名" >
          <template slot-scope="scope">
            {{ scope.row.code }}
          </template>
        </el-table-column>
        <el-table-column label="角色">
          <template slot-scope="scope">
            {{ scope.row.rname }}
          </template>
        </el-table-column>
        <el-table-column  label="时间" >
          <template slot-scope="scope">
            {{ scope.row.createTime|newDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="250" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="handleUpdate(row)">
             编辑
            </el-button>
            <el-button size="mini" type="danger" @click="deleteRole(row.rid)">
              删除
            </el-button>
            <el-button v-if="row.status!='deleted'" size="mini" type="danger">
              分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog width="400px" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
        <el-form ref="dataForm" :rules="rules" :model="user">
          <el-form-item label="姓名" :label-width="formLabelWidth" prop="username">
            <el-input v-model="user.code" />
          </el-form-item>
          <el-form-item label="角色名称" :label-width="formLabelWidth" prop="rname">
            <el-input v-model="user.rname" />
          </el-form-item>
          <el-form-item label="时间" :label-width="formLabelWidth" prop="createTime">
            <div class="block">
              <el-date-picker
                v-model="user.createTime"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import  axios from "axios";
    export default {
      name: "Role",
      data() {
        return {
          list:null,
          username:'',
          dialogFormVisible:false,
          dialogStatus:'',
          user:{
            code:'',
            rname:'',
            createTime:''
          },rules: {
            code:[
              {required:true,message:'请输入真实姓名',trigger:'blur'},
              {min:3,max:5,message: '长度在3到5个字符',trigger: 'blur'}
            ],
            rname:[
              {required:true,message:'请输入角色姓名',trigger:'change'},
              {min:3,max:5,message: '长度在3到5个字符',trigger: 'blur'}
            ],
            createTime: [
              {required: true, message: '请输入时间', trigger: 'blur'}
            ]
          },formLabelWidth: '80px',
          textMap:{
            update:'用户修改',
            create:'用户添加'
          }
        }
      }, created() {
        this.fetchDate()
      }, methods: {
        fetchDate() {
          axios.get(`http://localhost:8080/OAManger_war_exploded/getRoleList`).then(res => {
            this.list = res.data
          })
        },roleName(){
          axios.get(`http://localhost:8080/OAManger_war_exploded/getRoleName/`+this.username).then(
            res=>{
              this.list=res.data
            }
          )
        },resetTemp() {
          this.user = {
            code: '',
            rname: '',
            createTime: ''
          }
        },handleCreate() {
          // 清空表单
          this.resetTemp()
          this.dialogStatus = 'create'
          this.dialogFormVisible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
          })
        }, createData() {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              axios.post(`http://localhost:8080/OAManger_war_exploded/addRole`,
                this.user).then(
                res => {
                  // eslint-disable-next-line eqeqeq
                  if (res.data == 1) {
                    this.dialogFormVisible = false
                    this.fetchDate()
                    this.$notify({
                      title: '成功',
                      message: '添加成功',
                      type: 'success',
                      duration: 2000
                    })
                  }
                })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        },handleUpdate(row) {
          this.user = Object.assign({}, row) // copy obj
          console.log(this.user)
          this.dialogStatus = 'update'
          this.dialogFormVisible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
          })
        },updateData() {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              axios.post(`http://localhost:8080/OAManger_war_exploded/updateRole`,
                this.user
              ).then(res => {
                // eslint-disable-next-line eqeqeq
                if (res.data == 1) {
                  this.dialogFormVisible = false
                  this.fetchDate()
                  this.$notify({
                    title: '成功',
                    message: '修改成功',
                    type: 'success',
                    duration: 2000
                  })
                }
              })
            } else {
              console.log('error submit!!')
              return false
            }
          })
        },
        deleteRole(id){
          alert(id)
          axios.get(`http://localhost:8080/OAManger_war_exploded/deleteRole/`+id).then(
            res=>{
              this.fetchDate()
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              })
            }
          )
        }
      },
        filters: {
          newDate(createTime) {
            let d = new Date()
            return d.getFullYear() + "-" + d.getMonth() + "-" + d.getDate()
          }
        }
  }

</script>

<style scoped>

</style>
